package cn.virens.common.boot.mybatis.autoconfig;

import com.alibaba.druid.filter.Filter;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Getter
@Setter
@ConfigurationProperties(prefix = "spring.datasource.virens")
public class DynamicDataSourceProperties {
    private Map<String, DynamicDataSourceDruid> datasources;

    private Boolean enabled;
    
    private String primary;

    protected Collection<DynamicDataSourceDruid> allDataSource() {
        if (datasources == null || datasources.isEmpty()) {
            return Collections.emptyList();
        }

        return datasources.values();
    }

    public void addFilters(List<Filter> filters) {
        for (var item : allDataSource()) {
            item.addFilters(filters);
        }
    }

    public void closeAll() throws Exception {
        for (var item : allDataSource()) {
            item.close();
        }
    }

    public void initAll() throws Exception {
        for (var item : allDataSource()) {
            item.init();
        }
    }
}
